package com.gitee.srurl.system.util;

import com.gitee.srurl.core.util.IpUtil;
import com.gitee.srurl.core.util.ServletUtil;
import com.gitee.srurl.system.dto.RequestDeviceInfoDTO;
import eu.bitwalker.useragentutils.UserAgent;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * @author zxw
 * @date 2023/4/29 21:18
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class UserAgentUtil {

    public static RequestDeviceInfoDTO getRequestDeviceInfo() {
        HttpServletRequest request = ServletUtil.getRequest();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        String ip = IpUtil.getIpAddr(request);

        return RequestDeviceInfoDTO.builder()
                .os(os)
                .browser(browser)
                .ip(ip)
                .build();
    }
}
